#include <16F877A.H>
#include<stdio.h>
#include<stdlib.h>

/*========================================== Controller Configuration Settings========================================*/
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#define pinClock PIN_B2
#define pinData PIN_B1
#define pinBusy PIN_B0
#define pinReset PIN_B3

#define RF_A  PIN_D0
#define RF_B  PIN_D1
#define RF_C  PIN_D2
#define RF_D  PIN_D3

#define SELECT 0

const unsigned int Stop= 0xFFFF; // Stop
int Song; // current Song
unsigned int vol; // current volume

/**********************************************************************************
Send Sequence
**********************************************************************************/

void sendData(int TheSong)
{
int ClockCounter=0;
int ClockCycle=15;//0x0f;

output_high(pinClock); // Hold (idle) for 300msec to prepare data start
delay_ms(300);
output_low(pinClock); //Hold for 2msec to signal data start
delay_ms(2); //2msec delay

while(ClockCounter <= ClockCycle)
{
output_low(pinClock);

if (TheSong & 0x8000)
{
output_high(pinData);
}
else
{
output_LOW(pinData);
}

TheSong = TheSong << 1;
delay_us(200); //Clock cycle period is 200 uSec - LOW
output_high(pinClock);
ClockCounter++;
delay_us(200); //Clock cycle period is 200 uSec - HIGH
}
output_LOW(pinData);
output_high(pinClock); // Place clock high to signal end of data
}

/**********************************************************************************
Plays Stored Song by Number
**********************************************************************************/

void PlaySong(int SongNumber)
{
sendData(Stop);
sendData(SongNumber);
}

/**********************************************************************************
Reset SOMO
**********************************************************************************/
void Reset()
{
output_LOW(pinReset);
delay_ms(50);
output_high(pinReset);
Song=0;
}

void main()
{
int cmd;
port_b_pullups(true);
Reset();
while(1)
{
if(RF_A==SELECT)
{
PlaySong(4); //"sx"; x=Play Song Number
delay_ms(800); // Pause 2 seconds to show User selection
}
else if(RF_B==SELECT)
{
PlaySong(5); //"sx"; x=Play Song Number
delay_ms(800); // Pause 2 seconds to show User selection
}
else if(RF_C==SELECT)
{
PlaySong(1); //"sx"; x=Play Song Number
delay_ms(800); // Pause 2 seconds to show User selection
}
else if(RF_D==SELECT)
{
PlaySong(2); //"sx"; x=Play Song Number
delay_ms(800); // Pause 2 seconds to show User selection
}
}
}

